%include "nasm_macros.inc"
%include "os.inc"
section .text
BITS 16
%define SEGLDR  1000h
%define SEGLDR2 2000h
%define OFFLDR  0
%define OFFLDR2 0
%define LDR_SP  0ff00h
org OFFLDR
;org 100h
go:
;debug
mov bx,0102h; new|old active partition
cld
cli
mov sp,LDR_SP
mov ax,SEGLDR
mov ss,ax
mov ds,ax
mov es,ax
sti
push edx ;save cpu info
cmp bl,bh
je no_restore
call disk_reset
call disk_info
mov edx,DISK_FLOPPY
mov eax,buf
mov ebx,1   ;00HHCCCS;
mov ecx,SECT_SZ
xor si,si
call read_disk
mov edx,buf + PRT_START
rebk1:
test bl,bl
jz re1
add edx,4*4
dec bl
jmp rebk1
re1:
or byte [edx],80h
;
mov edx,buf + PRT_START
rebk2:
test bh,bh
jz re2
add edx,4*4
dec bh
jmp rebk2
re2:
and byte [edx],7fh
;
mov edx,DISK_FLOPPY
mov eax,buf
mov ebx,1   ;00HHCCCS;
mov ecx,SECT_SZ
call write_disk
no_restore:
;;;;;;;;;;;;;;;;;;;;;;;;;;; GO
;#1
;idt,gdt,tss,ldt(opt),1 page dir and 1 page table
;code seg with pm-code
;int and except handlers
;sys regs: GDTR, CR1 - CR4, MTRRs(P4,xeon,P6 only)
;CR0_PE=1
;#2
;TRY: flat memory model with/without paging; IA32e mode (if x64 processor)
;1.interrupts
;2.paging
;3.mutitasking
pop edx
;call PrintCpuInfo




mov ax,4f00h
mov dword [buf],'VBE2'
xor bx,bx
mov di,buf
int 10h
;call dump_regs
mov ax,di
call PrintVesaInfo
mov ax,vesa_mode
mov cx,1
call dump_dw
mov bx,[vesa_mode]
test bx,bx
jz novesa
mov ax,4f02h
int 10h
novesa:
call pr_crlf
mov ax,msg0
call debug
mov ax,chsok_intervals
xor ebx,ebx
xor ecx,ecx
call dump_dd
;call kb_read
mov ecx,dword [ldr2_sz]
mov ebx,dword [ldr2_chs]
mov di,chsok_intervals
push es
cmp dword [ds:di],0
jne jne1
call read_loader2nobad
jmp cont
jne1:
call read_loader2
cont:
pop es
;
call kb_read
jmp word SEGLDR2:OFFLDR2
;
exit:
cli
hlt
jmp $

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
read_loader2nobad:
pusha
push ds
push SEGLDR2 
pop es
mov ax,OFFLDR2
;
.go: ;new interv start
;es:ax = buf,ebx=chs,ecx=nbytes,dx=disk, ds:si=off sect last sect read
push ecx
mov edx,DISK_FLOPPY
mov ecx,SECT_SZ
push edx
mov si,sp
call read_disk
pop ebx
add ax,SECT_SZ
jnc .jnc
mov ax,es
add ax,1000h
mov es,ax
mov ax,OFFLDR2 
.jnc:
pop ecx
sub ecx,SECT_SZ
jns .go
p eax
mov ax,sp
mov cx,1
call dump_dw
pop eax
clc
pop ds
popa
ret
.fex:
pop ds
mov ax,msge0
call debug
stc
popa
ret


;;;;;;;;
read_loader2:
pusha
;i3
push ds
push SEGLDR2 
pop es
mov ax,OFFLDR2
;
.go: ;new interv start
mov ebx,dword [ds:di]
add di,4
or ebx,ebx
jz .ex
js .go
.bk:
p edx
mov si,sp
mov edx,DISK_FLOPPY
mov ecx,SECT_SZ
add ax,SECT_SZ
jnc .jnc
mov ax,es
add ax,1000h
mov es,ax
mov ax,OFFLDR2 + SECT_SZ
.jnc:
sub ax,SECT_SZ
call read_disk
jc .fex
add ax,SECT_SZ
pop edx
cmp dword [ds:di],0
je .ex
cmp dword [ds:di],-1
je .je2
cmp edx,dword [ds:di]
mov ebx,edx
jne .bk
.je2:
add di,4
jmp .go
.ex:
clc
pop ds
sub ax,SECT_SZ
and eax,0ffffh
call pr_crlf
p eax
mov ax,sp
mov cx,1
call dump_dw
pop eax
mov ax,msg2
call print
popa
ret
.fex:
stc
pop ds
mov ax,msge0
call debug
popa
ret

%include "procs.asm"
;;;;DATA
msg0 db 'reading Loader2...', 0
msg2 db ' bytes have been read',0
msge0 db 'reading error...',0


%if 0
;;;HELP
;To register GDT, LDT, IDT, TSS:
lgdt osGDTR/sgdt osGDTR
lldt ax/sldt ax 
lidt osIDTR / sidt osIDTR
ltr ax/str ax

;Checking privileges:
arpl ds,cs ;may ds++, if RPL(ds) < RPL(cs), ZF=1; else ZF=0
verr ds/verw ds ;ZF=1: read/write access granted for CPL-->DPL(ds); ZF=0: denyed

;Get descriptor fields
lar ax,ds ;copy AR(access rights) byte of desc(ds) to ax
lsl eax,ds ;copy segment limit (=size -1) from desc(ds) to ax in bytes(!)

;Tasks
clts ;clear CR0_TS flag

;Cache
invd 	;clear cache 
wbinvd  ;write cache to memory and clear cache
invlpg [adr] ;delete TLB entry for a page in which adr resides.

;MSR
rdmsr/wrmsr ; read/write MSR#ecx to/from (EDX|EAX)

;Syscall for non-segmented memory model.
sysenter ;cs:=MSR#174h, eip:=MSR#176h, ss:=cs+8, esp:=MSR#175h;
		 ;sysenter changes desc(cs), desc(ss): RPL=0, DPL=0, lim=4Gb, base=0, conform
sysexit  ;cs:=MSR#174h+16, ss:=MSR#174h+24, eip=edx,esp=ecx;
		 ;sysexit changes desc(cs), desc(ss): RPL=3, DPL=3, CPL=3, lim=4Gb, base=0, non-conform

%endif
;DATA
ldr2_chs dd 0
ldr2_sz dd 0
chsok_intervals times MAX_CHSINTS_SZ db 0 ;array
;db 1,1,0,0,6,2,0,0,-1,-1,-1,-1,8,2,0,0,11h,6,0,0,0,0,0,0,0,0,0

;BSS
buf equ $
